const mongoose = require('mongoose');
const { get } = require('./user');
module.exports={
    async add(options){
        if(!options.title||!options.content){
            throw new Error('标题，内容都不能为空！')
        }
        const NotepadModel=mongoose.models['Notepad'];
        await NotepadModel.create({
            userId:options.self._id.toString(),
            title:options.title,
            content:options.content,
            editTimeTick:new Date().getTime()
        });
    },
    async edit(options){
        if(!options.title||!options.content){
            throw new Error('标题，内容都不能为空！')
        }
        const NotepadModel=mongoose.models['Notepad'];
        const Notepad=await NotepadModel.findById(options._id);
        if(!!Notepad){
            Notepad.title=options.title;
            Notepad.content=options.content;
            Notepad.editTimeTick=new Date().getTime();
            await Notepad.save();
        }
    },
    async list(options){
        const NotepadModel=mongoose.models['Notepad'];
        let result=await NotepadModel.find({
            userId:options.self._id.toString(),
        },{title:1,editTimeTick:1});
        return result;
    },
    async delete(options){
        const NotepadModel=mongoose.models['Notepad'];
        const Notepad=await NotepadModel.findById(options._id);
        if(!!Notepad){
            await Notepad.delete();
        }
    },
    async get(options){
        const NotepadModel=mongoose.models['Notepad'];
        const Notepad=await NotepadModel.findById(options._id);
        return Notepad;
    }
}